<?php
/**
 * class HTML
 * @author pntrinh87@gmail.com
 * @version 2010-11-22
 */
class HTML
{
    var $title              = '';
    var $metakeywords       = '';
    var $metadescription    = '';
    var $domain             = '';
    var $titlesite          = '';
    var $year               = '2010';
    var $charset            = 'utf-8';
    var $subfolder          = '/';
    var $endline            = "";
    var $styles             = array();
    var $scripts            = array();
    var $arrUrl             = array();
    var $countUrl           = 0;    
    var $urlRoot            = '';
    var $linkhere           = '';
    var $category           = array();    
    
   
    
    /**
     * function setTitle($title)
     * @param $title: title of page
     */
    function setTitle($title)
    {
        $this->title  = $title;
    }
    
    /**
     * function setTitleSite($title)
     * 
     */
    function setTitleSite($title)
    {
        $this->titlesite = $title;
    }
    
    function setKeywords($keywords)
    {
        $this->metakeywords = $keywords;
    }
    
    function setDescription($description)
    {
        $this->metadescription = $description;
    }
    
    function setDomain($domain)
    {
        $this->domain = $domain;
    }
    
    function setSubfolder($folder)
    {
        $this->subfolder = $folder;
    }
    
    function setCharset($charset)
    {
        $this->charset = $charset;
    }
    
    function addCss($file)
    {
        $this->styles[] = $file;
    }
    
    function addJs($file)
    {
        $this->scripts[] = $file;
    }
    
    function ParseUrl($url)
    {
        global $folder_path_root;
        
        $url              = strtolower($url);
        $url              = preg_replace("(^($folder_path_root))","",$url);
        $this->arrUrl     = explode("/",$url);
        $this->countUrl   = count($this->arrUrl);
        $this->urlRoot    = $this->arrUrl[0];
        
        if($this->countUrl>1 && empty($this->arrUrl[$this->countUrl-1]))
        {
            $this->countUrl-=1;
        }
    }
    
    function LoadData()
    {
        $this->category[0]  = $this->LoadCategory($this->arrUrl[0],0);
        $type               = strtolower($this->category[0]['cType']);
        
        for($i=1;$i<$this->countUrl;$i++)
        { 
            if($type=='news')
            {
                if($i==2)
                {
                    $this->category[$i]=$this->LoadNews($this->arrUrl[$i]);
                }else
                {
                    $this->category[$i] = $this->LoadCategory($this->arrUrl[$i],$this->category[$i-1]['iId']);
                } 
            }
            else if($type=="tourtype")
            {
                if($i==1)
                {
                    $this->category[$i]=$this->LoadTourType($this->arrUrl[$i]);    
                }else
                {
                    $this->category[$i]=$this->LoadTour($this->arrUrl[$i]);  
                }                
            }
            else if($type=="hotel")
            {
                if($this->arrUrl[1]=='inquiryhotel')
                {
                    $this->countUrl--;
                    $this->category[1]          = $this->LoadCategory($this->arrUrl[1],$this->category[$i-1]['iId']);
                    $this->category[1]['hotel']  = $this->LoadHotel($this->arrUrl[2]);                    
                    $this->setContent('type','inquiryhotel');
                }
                else if($i==1 && strpos($this->arrUrl[$i],'.htm')===false)
                {
                    $this->category[$i] = $this->LoadLocation($this->arrUrl[$i]);
                }else
                {
                    $this->category[$i] = $this->LoadHotel($this->arrUrl[$i]);   
                }
            }
            else if($type=="location")
            {
                $this->category[$i] = $this->LoadLocation($this->arrUrl[$i]);
            }
            else if($type=="review")
            {
                $this->category[$i] = $this->LoadReview($this->arrUrl[$i]);
            }
            else if($type=="product")
            {
                if(strpos($this->arrUrl[$i],'.htm')!==false)
                {
                    $this->category[$i] = $this->LoadProduct($this->arrUrl[$i],$this->category[$i-1]['iId']);
                }
                else if($this->arrUrl[$i]=='new')
                {
                    $this->category[$i]['cName'] = getLang('New Product');
                    $this->category[$i]['type']  = 'new';
                }
                else if($this->arrUrl[$i]=='hot')
                {
                    $this->category[$i]['cName'] = getLang('Hot Product');
                    $this->category[$i]['type']  = 'hot';
                }
                else if($this->arrUrl[$i]=='typical')
                {
                    $this->category[$i]['cName'] = getLang('Typical Product');
                    $this->category[$i]['type']  = 'typical';
                }
                else
                {
                    $this->category[$i] = $this->LoadCatProduct($this->arrUrl[$i]);
                }      
            }
            else if($type=="tour")
            {
                if($this->arrUrl[1]=='inquiry')
                {
                    $this->countUrl--;
                    $this->category[1]          = $this->LoadCategory($this->arrUrl[1],$this->category[$i-1]['iId']);
                    $this->category[1]['tour']  = $this->LoadTour($this->arrUrl[2]);                    
                    $this->setContent('type','inquiry');
                }
                else if($i==1)
                {
                    $this->category[$i] = $this->LoadLocation($this->arrUrl[$i]);
                }
                else if(strpos($this->arrUrl[$i],'.htm')!==false)
                {
                    $this->category[$i] = $this->LoadTour($this->arrUrl[$i]);
                }
                else
                {
                    $this->category[$i] = $this->LoadTourType($this->arrUrl[$i]);
                }                
            }
            else
            {     
                $this->category[$i] = $this->LoadCategory($this->arrUrl[$i],$this->category[$i-1]['iId']);
            }
        }  
        
        $this->setTitle($this->category[$this->countUrl-1]['cMetaTitle']);
        $this->setDescription($this->category[$this->countUrl-1]['cMetaDescription']);
        $this->setKeywords($this->category[$this->countUrl-1]['cMetaKeywords']);          
    }
    
    function LoadCategory($url,$idP=0)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        $sql_select = "select * from tblcategory where iParentId=$idP ";
        
        if(empty($url) && $idP == 0)
        {
            $sql_select .= " and cType = 'Home' ";
        }else
        {
            $sql_select .= " and cUrlString='$url' ";
        }
        $sql_select .=" and cLanguage='$language'";
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='category';
        return $result;
    }
    
    function LoadTourType($url)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        $sql_select = "select * from tbltourtype where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='tourtype';
        return $result;     
    }   
    
    function LoadTour($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblsettour where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='tour';
        return $result; 
    }
    
    function LoadProduct($url,$idP=0)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblproduct where cUrlString='$url'";
        if($idP>0)
        {
            $sql_select .= " and iParentId= $idP ";
        }
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        if($result)
        {
            $result['type']='product';
            $sql_update = "update tblproduct set iCountView=iCountView+1 where iId = ".$result['iId'];
            $dbsql->query($sql_update);
        }
        
        return $result; 
    }
    
    function LoadCatProduct($url)
    {
        global $dbsql,$language;
        $sql_select = "select * from tblproduct_category where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        return $result; 
    }
    
    function LoadNews($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblnews_content where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='news';
        return $result; 
    }
    
    function LoadLocation($url,$idP=0)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        if($url=="map")
        {
            return array("cMetaTitle"=>"Map","type"=>"map","cName"=>"Map");
        }
        $sql_select = "select * from tbllocation where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='location';
        return $result;        
    }
    
    function LoadReview($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblreview where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='review';
        return $result; 
    }
    
    function LoadHotel($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblhotel where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='hotel';
        return $result;        
    }
    
    function getModule()
    {
        $module     = strtolower($this->category[0]['cType']); 
        if($module=='home')
        {
            $module = 'index';
        }
        if($this->category[0]==false)
        {
            $module = false;
        }
        else if (!empty($module) && is_dir("source/" . $module) == false)
        {
            $module = "module";
        }
        
        return $module;
    }
    
    function getUrl($i=-1)
    {
        if($i==-1)
        {
            $i  = $this->countUrl-1;
        }
        return $this->arrUrl[$i];
    }
    
    function getCategory($i=-1,$back=0)
    {
        if($i==-1)
        {
            $i  = $this->countUrl -1 +$back;
        }
        if($i>$this->countUrl -1 || $i<0)
        {
            $i  = $this->countUrl -1;
        }
        return $this->category[$i];
    }
    
    function getId()
    {
        return $this->category[$this->countUrl-1]['iId'];
    }
    
    function getContent($field,$json=null,$title=null)
    {
        if($json!=null)
        {
            @include_once("class_json.php");
            return FJSON::decode($this->category[$this->countUrl-1][$field]);
        }
        $text   = $this->category[$this->countUrl-1][$field];
        if($title!=null && !empty($text))
        {
            return $title.$text;
        }
        return ($text);
    }
    
    function checkContent($field)
    {
        $text   = strip_tags($this->category[$this->countUrl-1][$field]);
        if(empty($text))
        {
            return false;
        }
        return true;
    }
    
    function setContent($field,$value)
    {
        $this->category[$this->countUrl-1][$field] = $value;
    }
    
    function getField($i,$f)
    {
        $text   = $this->category[$i][$f];
        return $text;
    }
    
    function getTitle($field,$ttadd=null)
    {
        global $language;
        
        $ttadd  = (empty($ttadd))? '': ' '.$ttadd;
        $title  = $this->getContent($field).$ttadd;
        $html   = '<div class="title">';
        $html  .= '<h1>'.($title).'</h1>';
        $html  .= '</div>';
        return $html;
    }
    
    function getLinkHere()
    {
        if(!empty($this->linkhere))
        {
            return $this->linkhere;
        }
        $linkhere   = '<span id="linkhere">';
        $split      = ' &raquo; ';
        $link       = '';
        for($i=0;$i<$this->countUrl;$i++)
        {
            $link       .= '/'.$this->category[$i]['cUrlString'];            
            if($i<$this->countUrl-1)
            {
                $linkhere   .= '<a class="phere" href="'.echoLink($link,false).'" title="'.$this->category[$i]['cName'].'">';
                $linkhere   .= $this->category[$i]['cName'].'</a>';
                $linkhere.= $split;
            }else if($this->category[$i]['type']!='product')
            {
                $linkhere   .= '<span>'.$this->category[$i]['cName'].'</span>';
            }
        }  
        $linkhere  .= '</span>';
        return $linkhere;
    }
    
    /**
     * function getHeader()
     * @return : html header 
     */
    function getHeader()
    {
        global $folder_path_root;
        
        $title  = (empty($this->title))? $this->getContent('cName') : $this->title;
        $title .= ' - '.$this->titlesite;
        $html   = '<head>';
        $html  .= '<title>'.$title.'</title>'.$this->endline;
        $html  .= '<meta name="description" content="'.$this->metadescription.'" />'.$this->endline;
        $html  .= '<meta name="keywords" content="'.$this->title.','.$this->metakeywords.'" />'.$this->endline;
        $html  .= '<meta name="copyright" content="'.$this->year.' '.$this->domain.'" />'.$this->endline;
        $html  .= '<meta name="owner" content="'.$this->domain.'"/>'.$this->endline;
        //$html  .= '<meta http-equiv="Reply-to" content="'.$this->domain.'" />'.$this->endline;
        $html  .= '<meta name="Robots" content="index, follow" />'.$this->endline;
        $html  .= '<meta http-equiv="Content-Type" content="text/html; charset='.$this->charset.'" />'.$this->endline;
        
        //build html style css:
        foreach($this->styles as $file)
        {
            $html .= '<link rel="stylesheet" href="'.$folder_path_root.'styles/'.$file.'" type="text/css" media="screen" />'.$this->endline;
        }
        
        //build html javascript
        foreach($this->scripts as $file)
        {
            $html .= '<script src="'.$folder_path_root.'javascript/'.$file.'" type="text/javascript"></script>'.$this->endline;
        }       
        
        
        if($this->getBrowser()=="ie")
        {
            $html .= '<!--[if IE 6]>';
            $html .= '<script src="'.$folder_path_root.'javascript/ie_png.js" type="text/javascript"></script>'.$this->endline;
            $html .= '<script>try{ie_png.fix(\'.menu a,.png,.newproduct,.hotproduct,.prtt\');}catch(err){}</script>';
            $html .= '<![endif]-->';            
        }
        
        //$html   .= $this->getBackground();
        $html   .= '<script type="text/javascript">var url_root="'.$folder_path_root.'";</script>';
        $html   .= '<link rel="shortcut icon" href="'.$folder_path_root.'favicon.ico" />';
        $html   .= '</head>';
        return $html;
    }
    
    function getBrowser()
    {
        $browser=strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($browser,"opera")!==false)
        {
        	$browser="opera";
        }
        else if(strpos($browser,"firefox")!==false)
        {
        	$browser="firefox";
        }
        else if(strpos($browser,"msie")!==false)
        {
        	$browser="ie";
        }
        else if(strpos($browser,"safari")!==false)
        {
        	$browser="safari";
        }
        return $browser;
    }
        
    function getBackgroundHome($width=1600,$time=7000)
    {
        if(!empty($this->urlRoot))
        {
            return;
        }
        
        $html   = '<div class="bghome fixed">';
        $html  .= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" >';
        $html  .= '<param name="allowScriptAccess" value="sameDomain" />';
        $html  .= '<param name="movie" value="'.echoLink('/images/homepage.swf?timep='.$time.'&amp;file=http://'.$_SERVER['SERVER_NAME'].'/home.xml&amp;w_width='.$width).'" />';
        $html  .= '<param name="quality" value="high" />';
        $html  .= '<param name="salign" value="lt" />';
        $html  .= '<param name="wmode" value="transparent"/>';
        $html  .= '<embed src="'.echoLink('images/homepage.swf',false).'" flashvars="timep='.$time.'&amp;file=http://'.$_SERVER['SERVER_NAME'].'/home.xml&amp;w_width='.$width.'" wmode="transparent" quality="high" salign="lt"  width="100%" height="100%"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
        $html  .= '</object></div>';
        return $html;
    }
    
    function getBackground()
    {
        global $folder_img_upload;
        $link   = $this->category[$this->countUrl-1]['cBackground'];
        if(empty($link))
        {
            $link   = echoLink("images/defaultbg.jpg");
        }
        else
        {
            $link   = echoLink($folder_img_upload.$link);    
        }
        return '<style type="text/css">body{background: #000 url('.$link.') no-repeat  fixed ;}</style>';
    }
    
    function getIntroImage()
    {
        global $folder_img_upload;
        $link   = $this->category[$this->countUrl-1]['cImage'];
        if(!empty($link))
        {
            return "/".$folder_img_upload.$link;
        }
        return false;
    }
    /**
     * function : getMenu($place=1,$split= ' | ')
     * @param $place: place in page
     * @param $split: split links
     */
    function getMenu($place=1,$split=' | ')
    {
        global $dbsql,$language;
        
        $place      = intval($place);
        
        $sql_select = "select cUrlString,cName from tblcategory where iStatus=1 and (iPlace = $place or iPlace = 6) 
                    and cLanguage='$language' order by iOrder";
        $rs         = $dbsql->query($sql_select);
        $html       = '';
        $rows       = $dbsql->num_rows($rs);
        $i          = 0;
        while($result = $dbsql->fetch_array($rs))
        {
            $url    = (strtolower($result['cUrlString'])=="home")? $url="/" : "/".$result['cUrlString'];
            $html.= "<a href='".echoLink($url,false)."'>".$result['cName']."</a>";
            $i++;
            if($i<$rows)
            {
                $html.='<span>'.$split.'</span>';
            }
        }
        return $html;
    }  
    /**
     * function : getMainMenu()
     * build menu drop down
     */
    function getMainMenu()
    {
        global $dbsql,$language;
        
        $sql_select = "select iId,cUrlString,cName,cType,cMetaTitle from tblcategory where iStatus=1 and (iPlace = 2 or iPlace=6) 
                        and cLanguage='$language' order by iOrder";
        $rs         = $dbsql->query($sql_select);
        
        $i          = 0;
        $mainMenu   = '';
        
        while($result = $dbsql->fetch_array($rs))
        {
            $i++; 
            $url    = (strtolower($result['cType'])=="home")? $url='/': '/'.$result['cUrlString'];
            $class  = ($url=='/'.$this->urlRoot)? 'class="se"' : '';
            $mainMenu.= "<a $class rel=\"m".$i."_subMenu\" href='".echoLink($url,false)."'";
            if($result['cType']=='product')
            {
                $mainMenu   .= ' onmouseover="getId(\'productcathead\').style.display=\'\'"';
            }
            $mainMenu.= '><span>'.($result['cName']).'</span></a>'.$this->endline;
                        
        }        
        $mainMenu  .= $childMenu;
        return $mainMenu;
    }
    
    function _BuildChildMenu($idP,$urlP,$type,$titleP,$i)
    {
        global $dbsql,$language;
        
        $type       = strtolower($type);
        $childMenu  = '';//  '<a href="'.$urlP.'/">'.getLang("View All").'</a>';   
        
        $sql_select = "select iId,cUrlString,cName from tblcategory where iStatus =1 
                         and iParentId =$idP ";
        if($type=="tour")
        {
            $sql_select = "select iId,cUrlString,cName from tbllocation where iParentId=0 ";
        }
        else if($type=="location" || $type=="hotel")
        {
            $sql_select = "select iId,cUrlString,cName from tbllocation where iStatus =1 and iParentId=0 ";
            $childMenu  = '<a href="'.$urlP.'/">'.($titleP).'</a>';
        }        
        $sql_select.= " and cLanguage='$language' order by iOrder";
        
        $rs         = $dbsql->query($sql_select);
        
        if($dbsql->num_rows($rs)==0)
        {
            return;
        }       
             
        while($result   = $dbsql->fetch_array($rs))
        {            
            if($type=="tour")
            {
                $result['cName'].=" ".getLang("Journeys");
            }
            $childMenu.= "<a href='".$urlP."/".$result['cUrlString']."/'>".htmlentities($result['cName'])."</a>";
        }
        return $childMenu;
    }
    /**
     * function : getConfig($name);
     * @name : name of config in db
     */
    function getConfig($name,$ent=false)
    {
        global $dbsql,$language;
        
        $sql_select = "select cValue from tblconfig where cName = '$name' and cLanguage='$language'";
        $result     = $dbsql->query_fetch($sql_select);
        if($ent==true)
        {
            $result['cValue'] = ($result['cValue']);
        }
        return $result['cValue'];
    }
    
    function getBox($name)
    {
        ImportClass('box');
        getBox($name);
    }
    
    function getScript($name)
    {
        ImportClass('script');
        getScript($name);
    }
}
?>